package org.msfj.hospital.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.msfj.common.annotation.Constants;
import org.msfj.common.base.BaseServiceImpl;
import org.msfj.common.enums.Enable;
import org.msfj.hospital.model.HospitalDept;
import org.msfj.hospital.service.HospitalDeptService;
import org.msfj.hospital.service.HospitalExpertService;
import org.msfj.hospital.vo.HospitalDeptVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 医院科室信息service实现类
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2018-08-10 14:12:03
 * @Version: 1.0
 * @Created in idea by autoCode
 */
@Service
@CacheConfig(cacheNames = Constants.CACHE_NAMESPACE + "hospitalDept")
public class HospitalDeptServiceImpl extends BaseServiceImpl<HospitalDept> implements HospitalDeptService {

    @Autowired
    HospitalExpertService hospitalExpertService;

    /**
     * 根据条件分页查询医院科室信息列表
     *
     * @param hospitalDeptVo
     * @param page           当前第几页
     * @param rows           每页多少条记录
     * @return PageInfo<HospitalDept>
     */
    @Override
    //@Cacheable
    public PageInfo<HospitalDeptVo> getByHospitalDept(HospitalDeptVo hospitalDeptVo, int page, int rows) {
        Example example = new Example(HospitalDept.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        createCriteria(hospitalDeptVo,criteria);
        example.orderBy("sortNo").asc();
        PageHelper.startPage(page, rows);
        List<HospitalDept> hospitalDeptList = super.getByExample(example);
        PageInfo hospitalDeptVoPageInfo = new PageInfo(hospitalDeptList);
        hospitalDeptVoPageInfo.setList(setOtherProperties(hospitalDeptList));
        return hospitalDeptVoPageInfo;
    }

    /**
     * 根据条件查询所有医院科室信息对象列表
     *
     * @param hospitalDeptVo
     * @return List<HospitalDept>
     */
    @Override
    //@Cacheable
    public List<HospitalDeptVo> getAllHospitalDept(HospitalDeptVo hospitalDeptVo) {
        Example example = new Example(HospitalDept.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        createCriteria(hospitalDeptVo,criteria);
        List<HospitalDeptVo> hospitalDeptVoList = setOtherProperties(super.getByExample(example));
        return hospitalDeptVoList;
    }

    /**
     * 根据条件查询医院科室信息的tree对象
     *
     * @return List<HospitalDeptVo>
     */
    @Override
    //@Cacheable
    public List<HospitalDeptVo> getHospitalDeptTree() {
        List<HospitalDeptVo> hospitalDeptVos = this.getAllHospitalDept(null);
        List<HospitalDeptVo> rootHospitalDepts = CollectionUtil.newArrayList();
        if (CollectionUtil.isNotEmpty(hospitalDeptVos)) {
            for (HospitalDeptVo hospitalDeptVo : hospitalDeptVos) {
                hospitalDeptVo.setLabel(hospitalDeptVo.getName());
                if (StrUtil.isNotBlank(hospitalDeptVo.getTreePosition())) {
                    String[] split = hospitalDeptVo.getTreePosition().split("&");
                    hospitalDeptVo.setLevel(split.length);
                } else {
                    hospitalDeptVo.setLevel(1);
                }
                if (ObjectUtil.equal(hospitalDeptVo.getParentId(), "0")) {
                    rootHospitalDepts.add(hospitalDeptVo);
                }
                for (HospitalDeptVo childVo : hospitalDeptVos) {
                    if (ObjectUtil.equal(childVo.getParentId(), hospitalDeptVo.getId())) {
                        if (ObjectUtil.isNull(hospitalDeptVo.getChildren())) {
                            List<HospitalDeptVo> children = CollectionUtil.newArrayList();
                            children.add(childVo);
                            hospitalDeptVo.setChildren(children);
                        } else {
                            hospitalDeptVo.getChildren().add(childVo);
                        }
                    }
                }
            }
        }
        return rootHospitalDepts;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param hospitalDept
     * @return DepartmentVo
     */
    @Override
    //@Cacheable
    public HospitalDeptVo setOtherProperties(HospitalDept hospitalDept) {
        HospitalDeptVo hospitalDeptVo = new HospitalDeptVo();
        BeanUtils.copyProperties(hospitalDept, hospitalDeptVo);
        Map<String, Integer> hospitalDeptExpertCountMap = hospitalExpertService.getHospitalDeptExpertCountMap();
        hospitalDeptVo.setExpertCount(hospitalDeptExpertCountMap.get(hospitalDeptVo.getId()));
        return hospitalDeptVo;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param hospitalDepts
     * @return List<HospitalDeptVo>
     */
    @Override
    //@Cacheable
    public List<HospitalDeptVo> setOtherProperties(List<HospitalDept> hospitalDepts) {
        List<HospitalDeptVo> hospitalDeptVos = new ArrayList<>();
        Map<String, Integer> hospitalDeptExpertCountMap = hospitalExpertService.getHospitalDeptExpertCountMap();
        if (CollectionUtil.isNotEmpty(hospitalDepts)) {
            for (HospitalDept hospitalDept : hospitalDepts) {
                HospitalDeptVo hospitalDeptVo = new HospitalDeptVo();
                BeanUtils.copyProperties(hospitalDept, hospitalDeptVo);
                hospitalDeptVo.setExpertCount(hospitalDeptExpertCountMap.get(hospitalDeptVo.getId()));
                hospitalDeptVos.add(hospitalDeptVo);
            }
        }
        return hospitalDeptVos;
    }

    private void createCriteria(HospitalDeptVo hospitalDeptVo, Example.Criteria criteria) {
        if (ObjectUtil.isNotNull(hospitalDeptVo)) {
            if (StrUtil.isNotBlank(hospitalDeptVo.getParentId())) {
                criteria.andEqualTo("parentId", hospitalDeptVo.getParentId());
            }
        }
    }
}
